<template>
  <!-- 编辑用户弹框 -->
  <el-dialog title="用户编辑" :visible="userEditDialogVisible" @close="onClose">
    <el-form :model="userEditForm">
      <el-form-item label="用户名" :label-width="userEditFormLabelWidth">
        <el-input v-model="userEditForm.username" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="角色" :label-width="userEditFormLabelWidth">
        <el-select v-model="roles" multiple placeholder="请选择">
          <el-option
            v-for="item in roleOptions"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="密码" :label-width="userEditFormLabelWidth">
        <el-input v-model="userEditForm.password"></el-input>
      </el-form-item>-->
      <el-form-item label="备注" :label-width="userEditFormLabelWidth">
        <el-input v-model="userEditForm.descr"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="handleSubmit">确认</el-button>
      <el-button type="danger" size="mini" @click="onCancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { editUser } from "@/api/user";
import { getRoleOptions } from "@/api/role";

export default {
  name: "UserEdit",
  props: ["userEditDialogVisible"],
  data() {
    return {
      //   listLoading: true,
      userEditForm: {
        id: "",
        username: "",
        // password: "",
        descr: "",
      },
      userEditFormLabelWidth: "100px",
      roles: [],
      roleOptions: [],
    };
  },
  methods: {
    reset() {
      this.userEditForm.id = "";
      this.userEditForm.username = "";
      // this.userEditForm.password = "";
      this.userEditForm.descr = "";
    },
    handleSubmit() {
      var roleIds = "";
      if(this.roles.length > 0){
        roleIds = this.roles.join(",")
      }
      
      var params = {
        roleIds: roleIds,
        id: this.userEditForm.id,
        // username: this.userEditForm.username,
        // password: this.userEditForm.password,
        descr: this.userEditForm.descr,
      };

      editUser(params).then((response) => {
        if (response.data == 1) {
          this.reset();
          this.$emit("success");
        } else {
          this.reset();
          this.$emit("failed", response.data.msg);
        }
      });
    },
    onCancel() {
      this.$emit("cancel");
    },
    onClose() {
      this.$emit("close");
    },
    setFormValues(record) {
      // console.log("---record=" + JSON.stringify(record));
      this.userEditForm.username = record.username;
      // this.userEditForm.password = record.password;
      this.userEditForm.descr = record.descr;
      this.userEditForm.id = record.id;

      if (record.roleOptions && record.roleOptions.length > 0) {
        var roleArr = [];
        for (var i = 0; i < record.roleOptions.length; i++) {
          roleArr.push(record.roleOptions[i].id);
        }
        // console.log(roleArr)
        this.roles = roleArr;
      } else {
        this.roles = [];
      }
    },
  },
  watch: {
    userEditDialogVisible() {
      if (this.userEditDialogVisible) {
        getRoleOptions().then((response) => {
          this.roleOptions = response.data;
        });
      }
    },
  },
};
</script>
